package com.ate.special3;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author Ate
 * @description:
 * @date :2020/11/16 14:06
 */
public class GiftView extends RelativeLayout {
    //礼物飞进的动画
    private static TranslateAnimation giftLayoutInAnim;

    //icon缩放的动画
    private static ScaleAnimation iconScaleAnim;

    //用户名飞进的动画
    private static TranslateAnimation userNameIn;

    //礼物名飞进的动画
    private static TranslateAnimation giftNameIn;


    private AnimMessage animMessage;

    public GiftView(Context context, AnimMessage message) {
        super(context);
        animMessage = message;
        init();
    }

    public GiftView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GiftView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        giftLayoutInAnim = (TranslateAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.gift_inm);

        iconScaleAnim = (ScaleAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.icon_scale);

        userNameIn = (TranslateAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.gift_username_in);

        giftNameIn = (TranslateAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.git_name_in);

        //获取最外层的线性布局
        LayoutInflater.from(getContext()).inflate(R.layout.item_gift_anim, this);
        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);

        setLayoutParams(layoutParams);

        final TextView giftNumView = findViewById(R.id.giftNum);
        final TextView sendUser = findViewById(R.id.send_user);
        final TextView giftName = findViewById(R.id.gift_name);
        final ImageView giftIcon = findViewById(R.id.ivGift);
        final RelativeLayout giftTextLayout = findViewById(R.id.rlparent);
        ImageView userIcon = findViewById(R.id.user_icon);

        giftName.setText(String.format(getContext().getResources().getString(R.string.gift_tip)
                            ,animMessage.getGiftName()));

        sendUser.setText(animMessage.getUserName());

        giftIcon.setImageResource(R.mipmap.live_red_packet);

        userIcon.startAnimation(iconScaleAnim);

        giftNumView.setTag(1);  //给数量控件设置标记

        animMessage.setUpdateTime(System.currentTimeMillis()); //设置时间标记

        setTag(animMessage); //设置本View的标识

        iconScaleAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                sendUser.setVisibility(VISIBLE);
                giftName.setVisibility(VISIBLE);
                giftTextLayout.setVisibility(VISIBLE);
                sendUser.startAnimation(userNameIn);
                giftName.startAnimation(giftNameIn);
                giftTextLayout.startAnimation(giftLayoutInAnim);
            }

            @Override
            public void onAnimationEnd(Animation animation) {


            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        giftLayoutInAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                giftNumView.setVisibility(VISIBLE);
                giftIcon.setVisibility(VISIBLE);
                giftNumView.setText("x"+giftNumView.getTag());
                //开始暴击
                startComboAnim(giftNumView);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    public void startComboAnim(final TextView giftNumView) {
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(giftNumView,"scaleX",2.0f,1.0f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(giftNumView,"scaleY",2.0f,1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(100);
        animatorSet.setInterpolator(new OvershootInterpolator());
        animatorSet.playTogether(animator1,animator2);
        animatorSet.start();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                ((AnimMessage)getTag()).setUpdateTime(System.currentTimeMillis());
                giftNumView.setTag((Integer)giftNumView.getTag()+1);
                if ((Integer)giftNumView.getTag() <= ((AnimMessage) getTag()).getGiftNum()){
                    ((TextView)giftNumView).setText("x"+giftNumView.getTag());
                    startComboAnim(giftNumView);
                }else {
                    ((AnimMessage)getTag()).setComboAnimationOver(true);
                    return;
                }


            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }

}
